package com.itheima.ui;

import cn.hutool.core.io.FileUtil;
import com.itheima.domain.User;
import jdk.dynalink.Operation;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Iterator;

public class RegisterJFrame extends JFrame implements MouseListener {
    ArrayList<User> allUsers = new ArrayList<>();
    JTextField username = new JTextField();
    JTextField password = new JTextField();
    JTextField againPassword = new JTextField();
    JButton submit = new JButton();
    JButton reset = new JButton();
    //JButton jDialog=new JButton();

    public RegisterJFrame(ArrayList<User> allUsers) {
        this.allUsers = allUsers;

        this.initJFrame();

        this.initView();

        this.setVisible(true);
    }

    private void initView() {
        JLabel usernameText = new JLabel(new ImageIcon("image\\register\\注册用户名.png"));
        usernameText.setBounds(85, 135, 80, 20);
        this.getContentPane().add(usernameText);

        this.username.setBounds(185, 135, 200, 30);
        this.getContentPane().add(username);

        JLabel passwordText = new JLabel(new ImageIcon("image\\register\\注册密码.png"));
        passwordText.setBounds(97, 193, 70, 20);
        this.getContentPane().add(passwordText);

        this.password.setBounds(185, 190, 200, 30);
        this.getContentPane().add(password);

        JLabel againRasswordText = new JLabel(new ImageIcon("image\\register\\再次输入密码.png"));
        againRasswordText.setBounds(64, 255, 95, 20);
        this.getContentPane().add(againRasswordText);

        this.againPassword.setBounds(185, 255, 200, 30);
        this.getContentPane().add(againPassword);

        this.submit.setIcon(new ImageIcon("image\\register\\注册按钮.png"));
        this.submit.setBounds(123, 310, 128, 47);
        this.submit.setBorderPainted(false);
        this.submit.setContentAreaFilled(false);
        this.submit.addMouseListener(this);
        this.getContentPane().add(this.submit);

        this.reset.setIcon(new ImageIcon("image\\register\\重置按钮.png"));
        this.reset.setBounds(256, 310, 128, 47);
        this.reset.setBorderPainted(false);
        this.reset.setContentAreaFilled(false);
        this.reset.addMouseListener(this);
        this.getContentPane().add(this.reset);

        JLabel background = new JLabel(new ImageIcon("image\\register\\background.png"));
        background.setBounds(2, 0, 470, 390);
        this.getContentPane().add(background);

        JButton jButton = new JButton(new ImageIcon(""));
    }

    private void initJFrame() {
        this.setSize(488, 430);
        this.setTitle("拼图游戏 V1.0注册");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(null);
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Object obj = e.getSource();
        String username = this.username.getText();
        String password = this.password.getText();
        System.out.println("用户输入的用户名" + username);
        System.out.println("用户输入的密码" + password);
        if (obj == this.submit) {
            if (this.username.getText().length() == 0 ||
                    this.password.getText().length() == 0 ||
                    this.againPassword.getText().length() == 0) {
                showJDialog("用户名和密码不能为空");
                return;
            }
            if (!this.password.getText().equals(this.againPassword.getText())) {
                this.showJDialog("两次密码输入不一致");
                return;
            }
            if (!this.username.getText().matches("[a-zA-Z0-9_-]{4,16}")) {
                this.showJDialog("用户名不符合规则");
                return;
            }
            if (!this.password.getText().matches("[a-zA-Z0-9_-]{6,}")) {
                this.showJDialog("密码不符合规则");
                return;
            }
            if (containsUsername(this.username.getText())) {
                this.showJDialog("用户名已经存在，请重新输入");
                return;
            }
            this.allUsers.add(new User(this.username.getText(), this.password.getText()));
            FileUtil.writeLines(this.allUsers, "D:\\javaSE\\src\\com\\itheima\\mypuzzle\\userinfo.txt", "UTF-8");
            this.showJDialog("注册成功");
            this.setVisible(false);
            new LoginJFrame();
        } else if (obj == reset) {
            this.username.setText("");
            this.password.setText("");
            this.againPassword.setText("");
        }

    }

    private boolean containsUsername(String username) {
        Iterator<User> iterator = allUsers.iterator();
        while (iterator.hasNext()) {
            User user = iterator.next();
            if (user.getUsername().equals(username)) {
                return true;
            }
        }
        return false;
    }

    private void showJDialog(String content) {
        JDialog jDialog = new JDialog();
        jDialog.setLocationRelativeTo(null);
        jDialog.setAlwaysOnTop(true);
        jDialog.setSize(200, 150);
        jDialog.setModal(true);
        JLabel jLabel = new JLabel(content);
        jLabel.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(jLabel);
        jDialog.setVisible(true);
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

}
